import { IAppOption } from "../typings"
import { getPushSettingApi, getMonitorCompanyCountApi } from "./api/monitor"
import { zcc } from "./utils/util"

// app.ts
App<IAppOption>({
  globalData: {
    userInfo: undefined,
    isLogin: false,
    // （债务人详情页面跳转到财产线索页面 参数
    companyPara: {
      debtorId: null, //（债务人详情页面跳转到） 财产线索页面查询的 债务人id
      companyName: "", // 财产线索页面查询的 企业名称
    },
    safeNavHeight: 64,
    safeAreaTop: 20,
    safeArea: null,
    screenHeight: 844,
    prevPage: "mine", //跳转到财产线索的页面名称
    permissions: [] // 用户权限
  },
  // 监听器
  watch: function (variate, method) {
    var obj = this.globalData;
    let val = obj[variate];
    Object.defineProperty(obj, variate, {
      set: function (value) {
        val = value;
        method(variate, value);
      },
      get: function () {
        return val;
      }
    })
  },
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    //
    const isLogin = wx.getStorageSync('isLogin') || false
    const userInfo = wx.getStorageSync('userInfo') || undefined
    this.globalData.permissions = wx.getStorageSync('permissions') || []
    if (isLogin && userInfo) {
      this.globalData.userInfo = userInfo
      this.globalData.isLogin = isLogin
      // wx.redirectTo({
      //   url: '/pages/index/index'
      // })
    } else {
      zcc.logout()
      // wx.redirectTo({
      //   url: '/pages/login/login'
      // })
    }
    //
    wx.getSystemInfo({
      success: (res) => {
        const isAndroid = res.platform === 'android'
        const isDevtools = res.platform === 'devtools'
        this.globalData.safeNavHeight = (isAndroid ? 48 : 44) + (res.safeArea.top > 0 ? res.safeArea.top : 35);

        this.globalData.safeAreaTop = res.safeArea.top;
        this.globalData.safeArea = res.safeArea;
        this.globalData.screenHeight = res.screenHeight;
        console.log(res)
      }
    })


    //产品需求（强制弹框）：获取监控的企业 第二次进入且登录状态下时提示
    try {
      if (this.globalData.isLogin) {
        getPushSettingApi().then(res => {
          if (!res.content.wxSended) {
            getMonitorCompanyCountApi().then(res => {
              if (res.content && res.content > 0) {
                wx.showModal({
                  title: '提示',
                  content: '是否打开微信消息提醒，不再错过新增监控消息',
                  success: (res) => {
                    if (res.confirm) {
                      wx.navigateTo({ url: "/pages/monitor/components/setting/setting" })
                    }
                  }
                });
              }
            })
          }
        })
      }
    } catch (error) { }
  },

})